package kr.hkit.androidcomplete03.ch22;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Ball {
	// 볼의 좌표
	int x, y;

	// 볼의 크기
	int rad;

	// 볼의 이동방향
	int dx, dy;

	// 볼의 색
	int color;

	// 볼이 벽에 부딫히는 횟수
	int count;

	// 새로운 볼 생성
	static Ball Create(int x, int y) {
		Random Rnd = new Random();
		Ball NewBall = new Ball();

		NewBall.x = x;
		NewBall.y = y;
		NewBall.rad = Rnd.nextInt(50);

		do {
			NewBall.dx = Rnd.nextInt(11) - 5;
			NewBall.dy = Rnd.nextInt(11) - 5;
		} while (NewBall.dx == 0 || NewBall.dy == 0);

		NewBall.count = 0;
		NewBall.color = Color.rgb(Rnd.nextInt(256), Rnd.nextInt(256),
				Rnd.nextInt(256));

		return NewBall;
	}

	void Move(int Width, int Height) {
		x += dx;
		y += dy;

		if (x < rad || x > Width - rad) {
			dx *= -1;
			count++;
		}

		if (y < rad || y > Height - rad) {
			dy *= -1;
			count++;
		}
	}

	void Draw(Canvas canvas) {
		Paint pnt = new Paint();
		pnt.setAntiAlias(true);

		int r;
		int alpha;

		for (r = rad, alpha = 1; r > 4; r--, alpha += 5) {
			pnt.setColor(Color.argb(alpha, Color.red(color),
					Color.green(color), Color.blue(color)));
			canvas.drawCircle(x, y, r, pnt);
		}
	}

}